package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.parameter.AttrGroupListParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;

import java.util.HashMap;
import java.util.Map;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(AttrGroupListParam params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        String key = params.getKey();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.like("attr_group_id", key)
                        .or().like("attr_group_name", key);
            });
        }
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("curPage", params.getPage());
        hashMap.put("limit", params.getLimit());
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(hashMap),
                    queryWrapper
            );
            return new PageUtils(page);
        } else {
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(hashMap),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }
}